package leetcode.linked_list;


// 回文链表
public class L_234_1 {
    public static boolean isPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast.next != null ) {
            slow = slow.next;
            fast = fast.next.next;
        }

        while (slow.next != null) {
            if (head.val == slow.next.val) {
                head = head.next;
                slow = slow.next;
            }
            else {
                return false;
            }
        }

        return true;
    }

    public  static class ListNode {
       int val;
       ListNode next;
       ListNode() {}
       ListNode(int val) { this.val = val; }
       ListNode(int val, ListNode next) { this.val = val; this.next = next; }
   }

    public static void main(String[] args) {
        ListNode l1 =new ListNode(5);
        ListNode l2 =new ListNode(4,l1);
        ListNode l3=new ListNode(3,l2);
        ListNode l4=new ListNode(4,l3);
        ListNode l5=new ListNode(5,l4);
        System.out.println(isPalindrome(l5));
    }
}
